﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

using Perseus;

using Hetymine.Executioner.Data;
using Hetymine.Executioner.Triggers;

namespace Hetymine.Executioner.Plugins.Triggers.DateTime {
    /// <summary>
    /// Interaction logic for DateTimeTriggerConfigurator.xaml
    /// </summary>
    public partial class DateTimeTriggerConfigurator : UserControl, IConfigurator<ITrigger> {
        private DateTimeTrigger _Trigger;
        private static DateTimeTriggerConfiguratorWindow _ConfiguratorWindow;

        public DateTimeTriggerConfigurator() {
            InitializeComponent();

            this.cSelected.Click += new RoutedEventHandler(Selected_Click);

            this.cEdit.Click += new RoutedEventHandler(Edit_Click);
        }

        private void Selected_Click(object sender, RoutedEventArgs e) {
            this._Trigger.IsSelected = this.cSelected.IsChecked.Value;
        }

        private void Edit_Click(object sender, RoutedEventArgs e) {
            try {
                if (DateTimeTriggerConfigurator._ConfiguratorWindow == null) {
                    DateTimeTriggerConfigurator._ConfiguratorWindow = new DateTimeTriggerConfiguratorWindow();
                    DateTimeTriggerConfigurator._ConfiguratorWindow.Owner = Executioner.ConfigWindow;
                    DateTimeTriggerConfigurator._ConfiguratorWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                }

                DateTimeTriggerConfigurator._ConfiguratorWindow.InitializeTrigger(this._Trigger);
                DateTimeTriggerConfigurator._ConfiguratorWindow.ShowDialog();
                this.UpdateLabel();
            }
            catch {
                // Most likely caused by lack of WPFToolkit
                Executioner.Sounds.Error();
            }
        }
        
        private void UpdateLabel() {
            string label;

            switch (this._Trigger.DateType) {
                case DateType.Weekly:
                    label = "Weekly at " + this._Trigger.DateTime.ToString("HH:mm:ss");

                    List<string> days = new List<string>();
                    
                    if ((this._Trigger.Days | Days.Monday) == this._Trigger.Days) {
                        days.Add("Mon");
                    }
                    if ((this._Trigger.Days | Days.Tuesday) == this._Trigger.Days) {
                        days.Add("Tue");
                    }
                    if ((this._Trigger.Days | Days.Wednesday) == this._Trigger.Days) {
                        days.Add("Wed");
                    }
                    if ((this._Trigger.Days | Days.Thursday) == this._Trigger.Days) {
                        days.Add("Thu");
                    }
                    if ((this._Trigger.Days | Days.Friday) == this._Trigger.Days) {
                        days.Add("Fri");
                    }
                    if ((this._Trigger.Days | Days.Saturday) == this._Trigger.Days) {
                        days.Add("Sat");
                    }
                    if ((this._Trigger.Days | Days.Sunday) == this._Trigger.Days) {
                        days.Add("Sun");
                    }

                    if (days.Count > 0) {
                        label += " on " + days.ToArray().Join(", ");
                    }
                    else {
                        label += " (no days selected)";
                    }
                    break;
                case DateType.Once:
                    label = "Once on " + this._Trigger.DateTime.ToString("yyyy-MM-dd") + 
                            " at " + this._Trigger.DateTime.ToString("HH:mm:ss");
                    break;
                case DateType.Daily:
                default:
                    label = "Daily at " + this._Trigger.DateTime.ToString("HH:mm:ss");
                    break;
            }

            this.cDateTime.Inlines.Clear();
            this.cDateTime.Inlines.Add(new Italic(new Run(label)));
        }
        
        #region IConfigurator<ITrigger> Members
        public void InitializeConfig(ITrigger config) {
            this._Trigger = config as DateTimeTrigger;
            this.UpdateLabel();           
        }
        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
